<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background: #f0f0f0;
        }
        .test-section {
            background: white;
            padding: 20px;
            margin: 20px 0;
            border-radius: 10px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>AI角色扮演聊天 - 功能测试</h1>
    
    <div class="test-section">
        <h2>1. 服务器连接测试</h2>
        <button onclick="testServerConnection()">测试服务器连接</button>
        <div id="serverResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>2. 角色列表测试</h2>
        <button onclick="testRolesAPI()">获取角色列表</button>
        <div id="rolesResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>3. 聊天功能测试</h2>
        <button onclick="testChatAPI()">测试聊天API</button>
        <div id="chatResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>4. 打开主应用</h2>
        <button onclick="openMainApp()">打开主应用</button>
    </div>

    <script>
        async function testServerConnection() {
            const resultDiv = document.getElementById('serverResult');
            resultDiv.innerHTML = '测试中...';
            
            try {
                const response = await fetch('/api/models');
                if (response.ok) {
                    const data = await response.json();
                    resultDiv.innerHTML = `✅ 服务器连接成功！<br>当前模型: ${data.current}<br>可用模型: ${data.models.map(m => m.name).join(', ')}`;
                } else {
                    resultDiv.innerHTML = `❌ 服务器响应错误: ${response.status}`;
                }
            } catch (error) {
                resultDiv.innerHTML = `❌ 连接失败: ${error.message}`;
            }
        }
        
        async function testRolesAPI() {
            const resultDiv = document.getElementById('rolesResult');
            resultDiv.innerHTML = '获取中...';
            
            try {
                const response = await fetch('/api/roles');
                if (response.ok) {
                    const data = await response.json();
                    resultDiv.innerHTML = `✅ 获取角色列表成功！<br>角色数量: ${data.length}<br>角色: ${data.map(r => r.name).join(', ')}`;
                } else {
                    resultDiv.innerHTML = `❌ 获取角色列表失败: ${response.status}`;
                }
            } catch (error) {
                resultDiv.innerHTML = `❌ 请求失败: ${error.message}`;
            }
        }
        
        async function testChatAPI() {
            const resultDiv = document.getElementById('chatResult');
            resultDiv.innerHTML = '测试中...';
            
            try {
                const response = await fetch('/api/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        roleId: 'socrates',
                        message: '你好，苏格拉底！'
                    })
                });
                
                if (response.ok) {
                    const data = await response.json();
                    resultDiv.innerHTML = `✅ 聊天API测试成功！<br>角色: ${data.role.name}<br>回复: ${data.response.substring(0, 100)}...`;
                } else {
                    const errorData = await response.json();
                    resultDiv.innerHTML = `❌ 聊天API测试失败: ${response.status}<br>错误: ${errorData.error}`;
                }
            } catch (error) {
                resultDiv.innerHTML = `❌ 请求失败: ${error.message}`;
            }
        }
        
        function openMainApp() {
            window.open('/', '_blank');
        }
    </script>
</body>
</html>
